/*
 * Serum
 *
 * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
 */

/**
 * Main panel
 */
Serum.Panel.Main = Ext.extend(Ext.Viewport, {

    /**
     * Constructor
     */
    constructor: function() {
        var _items = [];
        _items.push(new Ext.BoxComponent({
                        region: 'north',
                        el:     'header',
                        height: 68
                    }));
        _items.push(new Serum.Panel.Packages());
        _items.push(new Serum.Panel.Module.List({
                        region:     'center',
                        margins:    (Serum.Preferences.get().getValue('OPEN_POPUP') == 1) ? '0 5 5 0' : '0 0 5 0'
                    }, this));

        if (Serum.Preferences.get().getValue('OPEN_POPUP') == 1) {
            new Serum.Window.Module.Form();

        } else {
            _items.push(new Serum.Panel.Module.Form({
                            region: 'east',
                            width:  '50%'
                        }, this));
        }

        Serum.Panel.Main.superclass.constructor.call(this, {
            layout: 'border',
            items:  _items
        });

        Ext.get('my_account').on('click', this.onMyAccount);
        Ext.get('my_preferences').on('click', this.onMyPreferences);
    },

    /**
     * Changes page title
     *
     * @param   {String}    title
     */
    setTitle: function(title) {
        document.title = title;
    },

    /**
     * Handler
     */
    onMyAccount: function() {
        var win = null;

        var onSuccess = function() {
            window.location.reload();
        }

        var form = new Serum.Form.Module.Extra.MyAccount({}, { module: 'Admins', controller: 'Index', action: 'myAccount', saveAction: 'saveMyAccount' }, onSuccess);

        win = new Ext.Window({
            title:      __('base~window_myaccount_title'),
            width:      420,
            layout:     'form',
            closable:   true,
            resizable:  false,
            
            border:     false,
            autoScroll: true,
            modal:      true,

            items:      []
        });

        win.add(form);

        form.on('formbuilded', function() {
            win.show();
            win.doLayout();
        });

        form.preload();
        form.load();
    },

    /**
     * Handler
     */
    onMyPreferences: function() {
        var onSuccess = function() {
            window.location.reload();
        }

        var form = new Serum.Form.Module.Extra.MyPreferences(onSuccess);

        var win = new Ext.Window({
            layout:     'form',
            title:      __('base~window_mypreferences_title'),
            width:      420,
            closable:   true,
            resizable:  false,
            plain:      true,
            border:     false,
            autoScroll: true,
            
            items:      []
        });

        win.add(form);

        form.on('formbuilded', function() {
            win.show();
            win.doLayout();
        });

        form.preload();
        form.load();
    }
});